package de.koller.worlders.engine.entity.mo.wphysics;

import de.koller.oceanFramework.math.Vec;
import de.koller.oceanFramework.util.OceanSet;
import de.koller.worlders.WSea;
import de.koller.worlders.engine.entity.mo.MovingObject;
import de.koller.worlders.engine.tiles.MOTile;

public class WPhysicsRoot extends WSea {
	
	private final OceanSet<MovingObject> mos = new OceanSet<>();
	private final OceanSet<MOTile> dynamicColls = new OceanSet<>();

	private final WVelocity wVelocity = new WVelocity();
	private final WCollision wCollision = new WCollision( this );
	private final WPosition wPosition = new WPosition();
	
	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		for( MovingObject mo : mos ) {
			wVelocity.updateVelocity( mo, elapsedTime );
			wVelocity.updateWishedWay( mo, elapsedTime );
		}

		for( MovingObject mo : mos ) {
			Vec ww = mo.wpTransferObj.getWishWay().newInstance();
			Vec aw = new Vec();
			
			mo.wpTransferObj.getWishWay().set( ww.exclude( true, false ) );
			wCollision.updateCollision( mo, elapsedTime );
			aw.setX( mo.wpTransferObj.getWishWay().getX() );
			
			mo.wpTransferObj.getWishWay().set( ww.exclude( false, true ) );
			wCollision.updateCollision( mo, elapsedTime );
			aw.setY( mo.wpTransferObj.getWishWay().getY() );
			
			mo.wpTransferObj.getWishWay().set( aw );
		}
	
		for( MovingObject mo : mos )
			wPosition.updatePosition( mo );
		
		mos.update();
		dynamicColls.update();
	}
	
	public void removeMO( MovingObject element ) {
		mos.remove( element );
	}

	public void addMO( MovingObject element ) {
		mos.add( element );
	}
	
	public void removeDynCol( MOTile element ) {
		dynamicColls.remove( element );
	}

	public void addDynCol( MOTile element ) {
		dynamicColls.add( element );
	}

	public OceanSet<MOTile> getDynamicCollisionObj() {
		return dynamicColls;
	}
	
}
